//
//  MJShowTextController.m
//  测试1.1
//
//  Created by 王志盼 on 15/2/4.
//  Copyright (c) 2015年 王志盼. All rights reserved.
//

#import "MJShowTextController.h"
#import "MJWordJudge.h"
#import "MJWordData.h"

@interface MJShowTextController () <UIAlertViewDelegate,MJWordJudgeDelegate>
- (IBAction)clickExitItem:(id)sender;
- (IBAction)clickLevelItem:(id)sender;

@property (weak, nonatomic) IBOutlet UITextView *textView;
- (IBAction)clickZeroBnt;
- (IBAction)clickOneBnt;
- (IBAction)clickTwoBnt:(id)sender;
- (IBAction)clickThreeBnt;
- (IBAction)clickFourBnt;

- (IBAction)clickFiveBnt:(id)sender;

- (IBAction)clickSixBnt:(id)sender;

@property (nonatomic, strong) NSMutableArray *allLevel;
@property (nonatomic, strong) NSMutableArray *zeroLevel;
@property (nonatomic, strong) NSMutableArray *oneLevel;
@property (nonatomic, strong) NSMutableArray *twoLevel;
@property (nonatomic, strong) NSMutableArray *threeLevel;
@property (nonatomic, strong) NSMutableArray *fourLevel;
@property (nonatomic, strong) NSMutableArray *fiveLevel;
@property (nonatomic, strong) NSMutableArray *sixLevel;

@property (nonatomic, strong) NSMutableAttributedString *attrString;
@end

@implementation MJShowTextController

/**
 *  入口
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    self.attrString = attrString;
    [self.textView setAttributedText:self.attrString];
    
    [self.textView setEditable:NO];
    
    MJWordJudge *judge = [[MJWordJudge alloc] init];
    judge.delegate = self;
    self.allLevel = [[NSMutableArray alloc] init];
    self.zeroLevel = [[NSMutableArray alloc] init];
    self.oneLevel = [[NSMutableArray alloc] init];
    self.twoLevel = [[NSMutableArray alloc] init];
    self.threeLevel = [[NSMutableArray alloc] init];
    self.fourLevel = [[NSMutableArray alloc] init];
    self.fiveLevel = [[NSMutableArray alloc] init];
    self.sixLevel = [[NSMutableArray alloc] init];
    [judge searchWithString:self.article];
    
}
/**
 *  实现MJWordJudge的代理方法，在此方法里面将各个等级的单词放入不同的等级数组
 *
 */
- (void)wordJudgeWithRange:(NSRange)range
{
    NSString *p = [self.article substringWithRange:range];
    p = [p lowercaseString];  //将字符串转化为小写
    
    if ([self.words isWordDataWithString:p]) {   //判断单词库中是否有该单词等级
        NSDictionary *dict = self.words.dict;
        
        NSString *level = dict[p];
        
        [self.allLevel addObject:NSStringFromRange(range)];
        //NSLog(@"---%d---%d",range.length,range.location);
        int tmpLevel = [level intValue];
        if (tmpLevel <= 0) {
            [self.zeroLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 1) {
            [self.oneLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 2) {
            [self.twoLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 3) {
            [self.threeLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 4) {
            [self.fourLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 5) {
            [self.fiveLevel addObject:NSStringFromRange(range)];
        }
        if (tmpLevel <= 6) {
            [self.sixLevel addObject:NSStringFromRange(range)];
        }
    }
}
/**
 *  点击退出Item，显示弹框
 *
 */
- (IBAction)clickExitItem:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"确定退出该文章？" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
/**
 *  当点击按钮时，需要显示文章中所有有等级的单词(变红)
 *
 */
- (IBAction)clickLevelItem:(id)sender {
    
    for (NSString *string in self.allLevel) {
            NSRange range = NSRangeFromString(string);
            // 红色
            [self.attrString addAttribute:NSForegroundColorAttributeName
                               value:[UIColor redColor]
                               range:range];
            //NSLog(@"%@",range);
        }
    
    [self.textView setAttributedText:self.attrString];
}

- (IBAction)clickZeroBnt {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.zeroLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickOneBnt {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.oneLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickTwoBnt:(id)sender {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.twoLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickThreeBnt {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.threeLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickFourBnt {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.fourLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickFiveBnt:(id)sender {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.fiveLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}

- (IBAction)clickSixBnt:(id)sender {
    self.textView.textColor = [UIColor blackColor];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.article];
    for (NSString *string in self.sixLevel) {
        NSRange range = NSRangeFromString(string);
        // 红色
        [attrString addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:range];
    }
    [self.textView setAttributedText:attrString];
}
@end